import pylynchipsdk as sdk
from .common import *

class ImageMergeInfo:
    def __init__(self, frame, video_info, x, y, width, height):
        self.frame = frame
        self.video_info = video_info
        self.x = x
        self.y = y
        self.width = width
        self.height = height


class IpeParamBase:
    def __init__(self, model_width, model_height):
        # 模型宽高
        self.m_iModelWidth = model_width
        self.m_iModelHeight = model_height

        # 输入图片信息
        self.m_iImgWidth = 0
        self.m_iImgHeight = 0
        self.m_format = None

        # 创建ipe相关结构体
        # ipe输入信息
        self.m_oPicDescIn, ret = sdk.lyn_ipe_create_pic_desc()
        error_check(ret, "lyn_ipe_create_pic_desc m_oPicDescIn")
        # ipe输出信息
        self.m_oPicDescOut, ret = sdk.lyn_ipe_create_pic_desc()
        error_check(ret, "lyn_ipe_create_pic_desc m_oPicDescOut")
        # ipe配置信息
        self.m_oConfigDesc, ret = sdk.lyn_ipe_create_config_desc()
        error_check(ret, "lyn_ipe_create_config_desc m_oConfigDesc")

    def destory(self):
        # 释放ipe相关结构体
        ret = sdk.lyn_ipe_destroy_pic_desc(self.m_oPicDescIn)
        error_check(ret, "lyn_ipe_destroy_pic_desc")
        ret = sdk.lyn_ipe_destroy_pic_desc(self.m_oPicDescOut)
        error_check(ret, "lyn_ipe_destroy_pic_desc")
        ret = sdk.lyn_ipe_destroy_config_desc(self.m_oConfigDesc)
        error_check(ret, "lyn_ipe_destroy_config_desc")

    # 设置输入图片的信息（接口分开为了同一类型的图片该方法可只调一次）
    def set_img_info(self, width, height, format):
        self.m_iImgWidth = width
        self.m_iImgHeight = height
        self.m_format = format

    def merge_image(self, stream, output_data, input_datas):
        error_check(sdk.lyn_ipe_reset_pic_desc(self.m_oPicDescOut))
        error_check(sdk.lyn_ipe_reset_config_desc(self.m_oConfigDesc))

        error_check(sdk.lyn_ipe_set_output_pic_data(self.m_oPicDescOut, output_data))

        for input_data in input_datas:
            if input_data.frame is None:
                continue
            error_check(sdk.lyn_ipe_reset_pic_desc(self.m_oPicDescIn))
            error_check(
                sdk.lyn_ipe_set_input_pic_desc(
                    self.m_oPicDescIn,
                    input_data.frame.data,
                    input_data.video_info.width,
                    input_data.video_info.height,
                    self.m_format,
                )
            )
            error_check(
                sdk.lyn_ipe_set_crop_config(
                    self.m_oConfigDesc,
                    0,
                    0,
                    input_data.video_info.width,
                    input_data.video_info.height,
                )
            )
            error_check(
                sdk.lyn_ipe_set_output_rect(
                    self.m_oConfigDesc,
                    input_data.x,
                    input_data.y,
                    input_data.width,
                    input_data.height,
                    False,
                )
            )
            error_check(
                sdk.lyn_ipe_process_async(
                    stream, self.m_oPicDescIn, self.m_oPicDescOut, self.m_oConfigDesc
                )
            )

    # IPE处理虚函数
    def calc_param(self, stream, input_data, output_data):
        pass

